//
// Source code recreated from ConfigVo .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ht.modules.system.online.graphreport.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import com.ht.entity.Result;
import com.ht.query.QueryGenerator;
import com.ht.utils.oConvertUtils;
import com.ht.modules.system.online.graphreport.entity.OnlGraphreportTemplet;
import com.ht.modules.system.online.graphreport.entity.OnlGraphreportTempletItem;
import com.ht.modules.system.online.graphreport.service.IOnlGraphreportTempletItemService;
import com.ht.modules.system.online.graphreport.service.IOnlGraphreportTempletService;
import com.ht.modules.system.online.graphreport.vo.OnlGraphreportTempletPage;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping({"/online/graphreport/templet"})
public class OnlGraphreportTempletController {
    private static final Logger log = LoggerFactory.getLogger(OnlGraphreportTempletController.class);
    @Autowired
    private IOnlGraphreportTempletService onlGraphreportTempletService;
    @Autowired
    private IOnlGraphreportTempletItemService onlGraphreportTempletItemService;

    public OnlGraphreportTempletController() {
    }

    @GetMapping({"/list"})
    public Result<IPage<OnlGraphreportTemplet>> queryPageList(OnlGraphreportTemplet onlGraphreportTemplet, @RequestParam(name = "pageNo",defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize",defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        Result<IPage<OnlGraphreportTemplet>> result = new Result<>();
        QueryWrapper<OnlGraphreportTemplet> queryWrapper = QueryGenerator.initQueryWrapper(onlGraphreportTemplet, req.getParameterMap());
        Page<OnlGraphreportTemplet> page = new Page((long)pageNo, (long)pageSize);
        IPage<OnlGraphreportTemplet> pageList = this.onlGraphreportTempletService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    @PostMapping({"/add"})
    public Result<OnlGraphreportTemplet> add(@RequestBody OnlGraphreportTempletPage onlGraphreportTempletPage) {
        Result result = new Result<>();

        try {
            OnlGraphreportTemplet onlGraphreportTemplet = new OnlGraphreportTemplet();
            BeanUtils.copyProperties(onlGraphreportTempletPage, onlGraphreportTemplet);
            this.onlGraphreportTempletService.saveMain(onlGraphreportTemplet, onlGraphreportTempletPage.getOnlGraphreportTempletItemList());
            result.success("添加成功！");
        } catch (Exception var4) {
            log.error(var4.getMessage(), var4);
            result.error500("操作失败");
        }

        return result;
    }

    @PutMapping({"/edit"})
    public Result<OnlGraphreportTemplet> edit(@RequestBody OnlGraphreportTempletPage onlGraphreportTempletPage) {
        Result<OnlGraphreportTemplet> result = new Result<>();
        OnlGraphreportTemplet onlGraphreportTemplet = new OnlGraphreportTemplet();
        BeanUtils.copyProperties(onlGraphreportTempletPage, onlGraphreportTemplet);
        OnlGraphreportTemplet onlGraphreportTempletEntity = (OnlGraphreportTemplet)this.onlGraphreportTempletService.getById(onlGraphreportTemplet.getId());
        if (onlGraphreportTempletEntity == null) {
            result.error500("未找到对应实体");
        } else {
            this.onlGraphreportTempletService.updateById(onlGraphreportTemplet);
            this.onlGraphreportTempletService.updateMain(onlGraphreportTemplet, onlGraphreportTempletPage.getOnlGraphreportTempletItemList());
            result.success("修改成功!");
        }

        return result;
    }

    @DeleteMapping({"/delete"})
    public Result<OnlGraphreportTemplet> delete(@RequestParam(name = "id",required = true) String id) {
        Result<OnlGraphreportTemplet> result = new Result<>();
        OnlGraphreportTemplet onlGraphreportTemplet = (OnlGraphreportTemplet)this.onlGraphreportTempletService.getById(id);
        if (onlGraphreportTemplet == null) {
            result.error500("未找到对应实体");
        } else {
            this.onlGraphreportTempletService.delMain(id);
            result.success("删除成功!");
        }

        return result;
    }

    @DeleteMapping({"/deleteBatch"})
    public Result<OnlGraphreportTemplet> deleteBatch(@RequestParam(name = "ids",required = true) String ids) {
        Result<OnlGraphreportTemplet> result = new Result<>();
        if (ids != null && !"".equals(ids.trim())) {
            this.onlGraphreportTempletService.delBatchMain(Arrays.asList(ids.split(",")));
            result.success("删除成功!");
        } else {
            result.error500("参数不识别！");
        }

        return result;
    }

    @GetMapping({"/queryById"})
    public Result<OnlGraphreportTemplet> queryById(@RequestParam(name = "id",required = true) String id) {
        Result<OnlGraphreportTemplet> result = new Result<>();
        OnlGraphreportTemplet onlGraphreportTemplet = (OnlGraphreportTemplet)this.onlGraphreportTempletService.getById(id);
        if (onlGraphreportTemplet == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(onlGraphreportTemplet);
            result.setSuccess(true);
        }

        return result;
    }

    @GetMapping({"/queryTempletItemByMainId"})
    public Result<List<OnlGraphreportTempletItem>> queryOnlGraphreportTempletItemListByMainId(@RequestParam(name = "id",required = true) String id) {
        Result<List<OnlGraphreportTempletItem>> result = new Result<>();
        List<OnlGraphreportTempletItem> onlGraphreportTempletItemList = this.onlGraphreportTempletItemService.selectByMainId(id);
        result.setResult(onlGraphreportTempletItemList);
        result.setSuccess(true);
        return result;
    }

    @RequestMapping({"/exportXls"})
    public ModelAndView exportXls(HttpServletRequest request, HttpServletResponse response) {
        QueryWrapper queryWrapper = null;

        try {
            String paramsStr = request.getParameter("paramsStr");
            if (oConvertUtils.isNotEmpty(paramsStr)) {
                String deString = URLDecoder.decode(paramsStr, "UTF-8");
                OnlGraphreportTemplet onlGraphreportTemplet = (OnlGraphreportTemplet)JSON.parseObject(deString, OnlGraphreportTemplet.class);
                queryWrapper = QueryGenerator.initQueryWrapper(onlGraphreportTemplet, request.getParameterMap());
            }
        } catch (UnsupportedEncodingException var11) {
            log.error(var11.getMessage(), var11);
        }

        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        List<OnlGraphreportTempletPage> pageList = new ArrayList<>();
        List<OnlGraphreportTemplet> onlGraphreportTempletList = this.onlGraphreportTempletService.list(queryWrapper);
        Iterator var7 = onlGraphreportTempletList.iterator();

        while(var7.hasNext()) {
            OnlGraphreportTemplet onlGraphreportTemplet = (OnlGraphreportTemplet)var7.next();
            OnlGraphreportTempletPage vo = new OnlGraphreportTempletPage();
            BeanUtils.copyProperties(onlGraphreportTemplet, vo);
            List<OnlGraphreportTempletItem> onlGraphreportTempletItemList = this.onlGraphreportTempletItemService.selectByMainId(onlGraphreportTemplet.getId());
            vo.setOnlGraphreportTempletItemList(onlGraphreportTempletItemList);
            pageList.add(vo);
        }

        mv.addObject("fileName", "Online报表多数据源主表列表");
        mv.addObject("entity", OnlGraphreportTempletPage.class);
        mv.addObject("params", new ExportParams("Online报表多数据源主表列表数据", "导出人:Jeecg", "导出信息"));
        mv.addObject("data", pageList);
        return mv;
    }

    @RequestMapping(
        value = {"/importExcel"},
        method = {RequestMethod.POST}
    )
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        Iterator var5 = fileMap.entrySet().iterator();
        if (var5.hasNext()) {
            Entry<String, MultipartFile> entity = (Entry)var5.next();
            MultipartFile file = (MultipartFile)entity.getValue();
            ImportParams params = new ImportParams();
            params.setTitleRows(2);
            params.setHeadRows(1);
            params.setNeedSave(true);

            Result var10;
            try {
                List<OnlGraphreportTempletPage> list = ExcelImportUtil.importExcel(file.getInputStream(), OnlGraphreportTempletPage.class, params);
                Iterator var23 = list.iterator();

                while(var23.hasNext()) {
                    OnlGraphreportTempletPage page = (OnlGraphreportTempletPage)var23.next();
                    OnlGraphreportTemplet po = new OnlGraphreportTemplet();
                    BeanUtils.copyProperties(page, po);
                    this.onlGraphreportTempletService.saveMain(po, page.getOnlGraphreportTempletItemList());
                }

                var10 = Result.ok("文件导入成功！数据行数：" + list.size());
                return var10;
            } catch (Exception var21) {
                log.error(var21.getMessage(), var21);
                var10 = Result.error("文件导入失败！");
            } finally {
                try {
                    file.getInputStream().close();
                } catch (IOException var20) {
                    var20.printStackTrace();
                }

            }

            return var10;
        } else {
            return Result.error("文件导入失败！");
        }
    }
}
